package com.ac.common.core.net;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;
import java.util.Map;

/**
 * @author : acfan
 * date : create in 2023/7/26 13:04
 * description :
 **/

public class HttpRequest {

    public static <T> ResponseEntity<T> sendGetForEntity(String url, Class<T> clazz) {
        return sendGetForEntity(url, null, clazz);
    }

    public static <T> ResponseEntity<T> sendGetForEntity(String url, Map<String, String> params, Class<T> clazz) {
        RestTemplate restTemplate = new RestTemplate();

        // 使用UriComponentsBuilder构建带有参数的URL
        if (params != null && !params.isEmpty()) {
            UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
            params.forEach(builder::queryParam);
            url = builder.build().toString();
        }

        // 如果找到MappingJackson2HttpMessageConverter，则设置ObjectMapper的命名策略
        List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
        converters.stream()
                .filter(converter -> converter instanceof MappingJackson2HttpMessageConverter)
                .map(converter -> (MappingJackson2HttpMessageConverter) converter)
                .findFirst().ifPresent(jacksonConverter -> jacksonConverter.getObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE));

        // 发送HTTP请求并接收响应
        return restTemplate.getForEntity(url, clazz);
    }

    public static <T> ResponseEntity<T> sendPostForEntity(String url, Map<String, Object> params, Class<T> clazz) {
        return sendPostForEntity(url, null, params, clazz);
    }

    public static <T> ResponseEntity<T> sendPostForEntity(String url, Map<String, String> headers, Map<String, Object> params, Class<T> clazz) {
        RestTemplate restTemplate = new RestTemplate();

        // 设置Header
        HttpHeaders httpHeaders = new HttpHeaders();
        if (headers != null && !headers.isEmpty()) {
            headers.forEach(httpHeaders::add);
        }

        // 设置请求参数
        HttpEntity<String> requestEntity = new HttpEntity<>(getBody(params), httpHeaders);

        // 查找MappingJackson2HttpMessageConverter，并设置ObjectMapper的命名策略
        List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
        converters.stream()
                .filter(converter -> converter instanceof MappingJackson2HttpMessageConverter)
                .map(converter -> (MappingJackson2HttpMessageConverter) converter)
                .findFirst().ifPresent(jacksonConverter -> jacksonConverter.getObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE));

        // 发送HTTP请求并接收响应
        return restTemplate.exchange(url, HttpMethod.POST, requestEntity, clazz);
    }

    // 构建请求Body
    private static String getBody(Map<String, Object> params) {
        if (params != null && !params.isEmpty()) {
            StringBuilder bodyBuilder = new StringBuilder();
            params.forEach((key, value) -> {
                if (bodyBuilder.length() > 0) {
                    bodyBuilder.append("&");
                }
                bodyBuilder.append(key).append("=").append(value);
            });
            return bodyBuilder.toString();
        }
        return "";
    }
}
